# leetcode 513 找树左下角的值


# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
        # 使用层序遍历
        # 每遍历一层，更新并记录该层第一个值
        # 遍历结束，返回记录值
        queue = []
        queue.append(root)
        result = root.val
        while queue:
            size = len(queue)
            result = queue[0].val
            while size:
                node = queue.pop(0)
                size -= 1
                if node.left:
                    queue.append(node.left)
                if node.right:
                    queue.append(node.right)
        return result
